#include "calServer.hpp"
#include <memory>
#include "Protocol.hpp"

using namespace server;
static void Usage(string proc)
{
    cout << "\nUsage:\n\t" << proc << " local_port\n\n";
}
enum
{
    OK = 0,
    DIV_ZERO,
    MOD_ZERO,
    OP_ERROR
};

// req:里面一定是我们的处理好的一个完整的请求对象
// resp:根据req,进行业务处理,填充resp,不用管理任何读取和写入,序列化和反序列化等任何细节
bool cal(const Request &req, Response &resp)
{
    // req已经有结构化完成的数据了,你可以直接使用
    resp.exitcode = 0;
    resp.result = 0;

    switch (req.op)
    {
    case '+':
        resp.result = req.x + req.y;
        break;
    case '-':
        resp.result = req.x - req.y;
        break;
    case '*':
        resp.result = req.x * req.y;
        break;
    case '/':
        if (req.y == 0)
            resp.exitcode = DIV_ZERO;
        else
            resp.result = req.x / req.y;
        break;
    case '%':
        if (req.y == 0)
            resp.exitcode = MOD_ZERO;
        else
            resp.result = req.x % req.y;
        break;
    default:
        resp.exitcode = OP_ERROR;
        break;
    }
    return true;
}

int main(int argc, char *argv[])
{
    if (argc != 2) //./tcpserver 8080
    {
        Usage(argv[0]);
    }
    uint16_t port = atoi(argv[1]); // 端口号通常占用 2 字节（16位)
    unique_ptr<TcpServer> tsvr(new TcpServer(port));

    tsvr->initServer();
    tsvr->start(cal);

    return 0;
}